Mid-term Exam
CS100 Midterm Exam Cover Sheet
Instructors: Laurent Kneip, Jie Zheng, Fu Song
November 16, 2018
INSTRUCTIONS:
- You have 90 minutes (~10:25-11:55) to complete the exam.
- Your exam will not be graded unless you complete the above section and the cover sheet, and turn in both this exam book and the cover sheet.
- This exam is closed-book and closed-notes, and no electronic devices are permitted.
- Mark your answers on the exam itself. We will not grade answers written on scratch paper.
- Your performance is supposed to reflect your own level of understanding of the material. You are not allowed to talk with your neighbor or look at his exam sheet. Failure to obey this rule will result in a point deduction.
- If you finish early, you can hand in the exam and leave early. However, this is only possible until at latest 15 minutes before the ending time of the exam. If less than 15 minutes are left, please keep sitting and wait until the end.
Problem 1: Multiple-choice questions (4 points)
Please answer the following questions by ticking the choices that apply. Note that multiple choices are possible for each question. Mark all choices that apply as follows: [x].
void f(int, short);
int main()
{
int i = 100;
short s = 12;
short *p = &s;
___________; // call to f()
return 0;
}
void f(int i, short s)
{
i++;
}
Which of the following expression(s), when inserted in the blank line above, will result in a compile-time error?
#include <stdio.h>
int main(void)
{
int arr[10][10][10];
arr[5][5][5] = 2018;
___________ // printf statement here
return 0;
}
Which of the following printf statement(s), when inserted in the blank line, would print 2018?
float add( float input );
Problem 2: Knowledge questions (4 points)
I am good.
”? Briefly explain your answer.
(You only need to type a simple number below.)
Put down a one-sentence statement explaining the purpose of include-guards.
Problem 3:
#include <stdio.h>
int f(int n);
int main()
{
printf(“Result = %d\n”, f(5));
return 0;
}
int f(int n)
{
int a, b;
if (n > 2) {
a = f(n-1);
b = f(n-2);
printf(“a = %d, b = %d\n”, a, b);
return a + b;
} else {
return 2;
}
}
Problem 4:
1. // This function reverses the characters in a string
2. void reverse(char *s)
3. {
4. int n;
5. char *tmp, *p, *q;
6. n = strlen(s);
7. q = (n > 0) ? (s+n) : s;
8. for (p=s; p < q; ++p, --q) {
9. *tmp = *p;
10. *p = *q;
11. *q = *tmp;
12. }
13. }
Problem 5: Inheritance and polymorphism (C++)
Imagine the following base class:
class Shape {
public:
Shape();
virtual ~Shape();
virtual float Area() = 0;
};
Shape::Shape(){}
Shape::~Shape(){}
a) Write down the declaration of a child class Square that inherits from the above abstract parent class Shape. The child class takes exactly one float parameter in the constructor, which denotes the edge length of the square. You may assume that everything is in one file, so you do not have to worry about header inclusion etc.
b) Write down the implementation of all procedures in the child class (including constructors and destructors).